package onlinejudge.service.impl;

import onlinejudge.dao.SolutionDao;
import onlinejudge.entity.Solution;
import onlinejudge.service.SolutionService;
import onlinejudge.util.DateUtil;
import onlinejudge.vo.SolutionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SolutionServiceImpl implements SolutionService {

    @Resource
    private SolutionDao solutionDao;
    private Map<String, Object> parameters = new HashMap<String, Object>();

    @Override
    public List<SolutionVo> querySolution(Map<String, Object> parameters1) {
        return solutionDao.querySolution(parameters1);
    }

    @Override
    public void addSolution(Solution solution) {
        parameters.clear();
        parameters.put("userId", solution.getUserId());
        parameters.put("problemId", solution.getProblemId());
        parameters.put("solutionContent", solution.getSolutionContent());
        parameters.put("code", solution.getCode());
        parameters.put("solutionDate", DateUtil.getDate());
        parameters.put("solutionViewCount", 0);
        solutionDao.addSolution(parameters);
    }

    @Override
    public void deleteSolution(Integer solutionId) {
        solutionDao.deleteSolution(solutionId);
    }

    @Override
    public Integer getSolutionCount() {
        parameters.clear();
        return solutionDao.querySolution(parameters).size();
    }

    @Override
    public SolutionVo getSolution(Integer solutionId) {
        parameters.clear();
        parameters.put("solutionId", solutionId);
        return solutionDao.querySolution(parameters).get(0);
    }

    @Override
    public void updateViewCount(Solution solution) {
        parameters.clear();
        parameters.put("solutionId", solution.getSolutionId());
        parameters.put("solutionViewCount", solution.getSolutionViewCount());
        solutionDao.updateSolution(parameters);
    }

}
